var pickedHandCards = []
var pickedBoardCards = []
var pickedOtherHandCards = []
var maxTestCnt = 5000

var date = new Date()
var Poker = function (n) {
  this.num = (n % 13) + 2;
  this.ch = this.num > 9 ? ("abcdef"[this.num - 10]) : this.num;
  this.show = "0,1,2,3,4,5,6,7,8,9,10,J,Q,K,A".split(",")[this.num];
  this.color = "♠♥♣♦"[n / 13 | 0];
  this.toString = function () {
    return this.color + this.show;
  };
};
var z = function (arg) { //同花顺
  var a = arg, t = a[0];
  for (var i = 1; i < a.length; i++) {
    if (a[i].color === t.color && a[i].num === t.num + 1) {
      t = a[i];
    } else if (i === a.length - 1 && t.num === 5 && a[i].num === 14 && a[i].color === t.color) {
      t = a[0];
    } else {
      return;
    }
  }
  return "z" + t.ch;
};
var y = function (arg) { //四条
  var a = arg.map(function (p) {
    return p.ch;
  }).join(""), mat = a.match(/(\w*)(\w)\2\2\2(\w*)/);
  if (mat) {
    return "y" + mat[2] + mat[1] + mat[3];
  }
};
var x = function (arg) { //葫芦
  var a = arg.map(function (p) {
    return p.ch;
  }).join(""), mat;
  if (mat = a.match(/(\w)\1(\w)\2\2/)) {
    return "x" + mat[2] + mat[1];
  } else if (mat = a.match(/(\w)\1\1(\w)\2/)) {
    return "x" + mat[1] + mat[2];
  }
};
var w = function (arg) { //同花
  var a = arg, t = a[0];
  for (var i = 1; i < a.length; i++) {
    if (a[i].color === t.color) {
      t = a[i];
    } else {
      return;
    }
  }
  return "w" + arg.map(function (p) {
    return p.ch;
  }).reverse().join("");
};
var v = function (arg) { //顺子
  var a = arg, t = a[0];
  for (var i = 1; i < a.length; i++) {
    if (a[i].num === t.num + 1) {
      t = a[i];
    } else if (i === a.length - 1 && t.num === 5 && a[i].num === 14) {
      t = a[0];
    } else {
      return;
    }
  }
  return "v" + t.ch;
};
var u = function (arg) { //三条
  var a = arg.map(function (p) {
    return p.ch;
  }).join(""), mat;
  if (mat = a.match(/(\w*)(\w)\2\2(\w*)/)) {
    return "u" + mat[2] + (mat[3] + mat[1]).split("").reverse().join("");
  }
};
var t = function (arg) { //两对
  var a = arg.map(function (p) {
    return p.ch;
  }).join(""), mat;
  if (mat = a.match(/(\w*)(\w)\2(\w)\3(\w*)/)) {
    return "t" + mat[3] + mat[2] + mat[4] + mat[1];
  } else if (mat = a.match(/(\w)\1(\w)(\w)\3/)) {
    return "t" + mat[3] + mat[1] + mat[2];
  }
};
var s = function (arg) { //一对
  var a = arg.map(function (p) {
    return p.ch;
  }).join(""), mat;
  if (mat = a.match(/(\w*)(\w)\2(\w*)/)) {
    return "s" + mat[2] + (mat[1] + mat[3]).split("").reverse().join("");
  }
};
var r = function (arg) { //高牌
  return "r" + arg.map(function (p) {
    return p.ch;
  }).reverse().join("");
};
var score = function () {
  var a = [].map.call(arguments.length == 1 ? arguments[0] : arguments, function (p) {
    return new Poker(p);
  }).sort(function (p1, p2) {
    return p1.num - p2.num;
  });
  return z(a) || y(a) || x(a) || w(a) || v(a) || u(a) || t(a) || s(a) || r(a);
};
var testCnt = 1
for (var i = 0; i < 2 - pickedHandCards.length; i++) {
  testCnt *= (52 - pickedHandCards.length - pickedBoardCards.length - pickedOtherHandCards.length - i) / (i + 1)
}
for (var i = 0; i < 5 - pickedBoardCards.length; i++) {
  testCnt *= (52 - 2 - pickedBoardCards.length - pickedOtherHandCards.length - i) / (i + 1)
}
for (var i = 0; i < 2 - pickedOtherHandCards.length; i++) {
  testCnt *= (52 - 2 - 5 - pickedOtherHandCards.length - i) / (i + 1)
}
var lose = 0
var handCards = []
pickedHandCards.forEach(card => {
  handCards.push(card)
})
var boardCards = []
pickedBoardCards.forEach(card => {
  boardCards.push(card)
})
var otherHandCards = []
pickedOtherHandCards.forEach(card => {
  otherHandCards.push(card)
})
for (var testNo = 0; testNo < maxTestCnt && testNo < testCnt; testNo++) {
  if (maxTestCnt < testCnt) {
    handCards.splice(pickedHandCards.length, 2)
    boardCards.splice(pickedBoardCards.length, 5)
    otherHandCards.splice(pickedOtherHandCards.length, 2)
    while (1) {
      var card = (Math.random() * 52 | 0) % 52
      if (handCards.indexOf(card) == -1 && boardCards.indexOf(card) == -1 && otherHandCards.indexOf(card) == -1) {
        if (handCards.length < 2) {
          handCards.push(card)
        } else if (boardCards.length < 5) {
          boardCards.push(card)
        } else if (otherHandCards.length < 2) {
          otherHandCards.push(card)
        } else {
          break
        }
      }
    }
  } else {
    if (testNo == 0) {
      for (var card = 0; card < 52; card++) {
        if (handCards.indexOf(card) == -1 && boardCards.indexOf(card) == -1 && otherHandCards.indexOf(card) == -1) {
          if (handCards.length < 2) {
            handCards.push(card)
          } else if (boardCards.length < 5) {
            boardCards.push(card)
          } else if (otherHandCards.length < 2) {
            otherHandCards.push(card)
          } else {
            break
          }
        }
      }
    } else {
      var finish = false
      while (1) {
        for (var i = 1; i >= pickedOtherHandCards.length; i--) {
          while (1) {
            otherHandCards[i]++
            if (handCards.indexOf(otherHandCards[i]) == -1 && boardCards.indexOf(otherHandCards[i]) == -1 && pickedOtherHandCards.indexOf(otherHandCards[i]) == -1)
              break
            if (otherHandCards[i] == 51 + i)
              break
          }
          if (otherHandCards[i] == 51 + i)
            continue
          finish = true
          for (i++; i < 2; i++) {
            otherHandCards[i] = otherHandCards[i - 1]
            while (1) {
              otherHandCards[i]++
              if (handCards.indexOf(otherHandCards[i]) == -1 && boardCards.indexOf(otherHandCards[i]) == -1 && pickedOtherHandCards.indexOf(otherHandCards[i]) == -1)
                break
              if (otherHandCards[i] == 51 + i)
                break
            }
            if (otherHandCards[i] == 51 + i) {
              finish = false
              break
            }
          }
          if (finish)
            break
        }
        if (finish)
          break
        for (var i = 4; i >= pickedBoardCards.length; i--) {
          while (1) {
            boardCards[i]++
            if (handCards.indexOf(boardCards[i]) == -1 && pickedBoardCards.indexOf(boardCards[i]) == -1 && pickedOtherHandCards.indexOf(boardCards[i]) == -1)
              break
            if (boardCards[i] == 48 + i)
              break
          }
          if (boardCards[i] == 48 + i)
            continue
          finish = true
          for (i++; i < 5; i++) {
            boardCards[i] = boardCards[i - 1]
            while (1) {
              boardCards[i]++
              if (handCards.indexOf(boardCards[i]) == -1 && pickedBoardCards.indexOf(boardCards[i]) == -1 && pickedOtherHandCards.indexOf(boardCards[i]) == -1)
                break
              if (boardCards[i] == 48 + i)
                break
            }
            if (boardCards[i] == 48 + i) {
              finish = false
              break
            }
          }
          if (finish) {
            for (var j = 0; j < 2; j++) {
              otherHandCards[j] = j == 0 ? -1 : otherHandCards[j - 1]
              while (1) {
                otherHandCards[j]++
                if (handCards.indexOf(otherHandCards[j]) == -1 && boardCards.indexOf(otherHandCards[j]) == -1 && pickedOtherHandCards.indexOf(otherHandCards[j]) == -1)
                  break
                if (otherHandCards[j] == 51 + j)
                  break
              }
              if (otherHandCards[j] == 51 + j) {
                finish = false
                break
              }
            }
            if (finish)
              break
          }
        }
        if (finish)
          break
        for (var i = 1; i >= pickedHandCards.length; i--) {
          while (1) {
            handCards[i]++
            if (pickedHandCards.indexOf(handCards[i]) == -1 && pickedBoardCards.indexOf(handCards[i]) == -1 && pickedOtherHandCards.indexOf(handCards[i]) == -1)
              break
            if (handCards[i] == 51 + i)
              break
          }
          if (handCards[i] == 51 + i)
            continue
          finish = true
          for (i++; i < 2; i++) {
            handCards[i] = handCards[i - 1]
            while (1) {
              handCards[i]++
              if (pickedHandCards.indexOf(handCards[i]) == -1 && pickedBoardCards.indexOf(handCards[i]) == -1 && pickedOtherHandCards.indexOf(handCards[i]) == -1)
                break
              if (handCards[i] == 51 + i)
                break
            }
            if (handCards[i] == 51 + i) {
              finish = false
              break
            }
          }
          if (finish) {
            for (var j = 0; j < 4; j++) {
              boardCards[j] = j == 0 ? -1 : boardCards[j - 1]
              while (1) {
                boardCards[j]++
                if (handCards.indexOf(boardCards[j]) == -1 && pickedBoardCards.indexOf(boardCards[j]) == -1 && pickedOtherHandCards.indexOf(boardCards[j]) == -1)
                  break
                if (boardCards[j] == 48 + j)
                  break
              }
              if (boardCards[j] == 48 + j) {
                finish = false
                break
              }
            }
            if (finish) {
              for (var j = 0; j < 2; j++) {
                otherHandCards[j] = j == 0 ? -1 : otherHandCards[j - 1]
                while (1) {
                  otherHandCards[j]++
                  if (handCards.indexOf(otherHandCards[j]) == -1 && boardCards.indexOf(otherHandCards[j]) == -1 && pickedOtherHandCards.indexOf(otherHandCards[j]) == -1)
                    break
                  if (otherHandCards[j] == 51 + j)
                    break
                }
                if (otherHandCards[j] == 51 + j) {
                  finish = false
                  break
                }
              }
              if (finish)
                break
            }
          }
        }
        if (finish)
          break
      }
    }
  }
  var bestScore = ""
  for (var i = 0; i < 7; i++) {
    for (var j = i + 1; j < 7; j++) {
      var cards = []
      handCards.forEach(card => {
        cards.push(card)
      })
      boardCards.forEach(card => {
        cards.push(card)
      })
      cards.splice(j, 1)
      cards.splice(i, 1)
      var thisScore = score(cards);
      if (bestScore < thisScore)
        bestScore = thisScore
    }
  }
  var hasResult = false
  for (var i = 0; i < 7; i++) {
    for (var j = i + 1; j < 7; j++) {
      var cards = []
      otherHandCards.forEach(card => {
        cards.push(card)
      })
      boardCards.forEach(card => {
        cards.push(card)
      })
      cards.splice(j, 1)
      cards.splice(i, 1)
      var thisScore = score(cards)
      if (bestScore < thisScore) {
        lose++
        hasResult = true
        break
      }
    }
    if (hasResult)
      break
  }
}
console.log((1 - lose / (maxTestCnt < testCnt ? maxTestCnt : testCnt)) + ' takes ' + (new Date().getTime() - date.getTime()) + ' ms')
